//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<expose_engine_to_python.cpp>
///	@path	~/src/python/wrap/
///	@date	2008/10/04
///	@desc	.

#include "python_pre.h"

#include "lib/utilities/observer.h"

#include "engine/xcore/xeres_engine.h"

using namespace xeres;

#include "python/wrap/expose_decl_python.h"

namespace xeres {

	namespace
	{
		// Timer callback execution wrap.
		void TimerWrap( float overdtime , object func )
		{
			try
			{
				func();
			}
			catch( error_already_set )
			{
				PyErr_Print();
				PyErr_Clear();
			}
		}

		// Timer set wrap.
		void TimerSetWrap( float period , object func )
		{
			if( !PyCallable_Check( func.ptr() ) )
			{
				PyErr_SetString( PyExc_ValueError, "Object is not callable." );
				boost::python::throw_error_already_set();
			}
			Engine::instance().InsertTimer( period , bind( &TimerWrap , _1 , func ) );
		}

		// Resolution wrap.
		Pair<int> ResolutionWrap( void )
		{
			return Pair<int>( Engine::instance().ResX<int>() , Engine::instance().ResY<int>() );
		}

		// Global time wrap.
		float GtimeWrap( void )
		{
			return Engine::instance().gtime();
		}

		// Diff time wrap.
		float DtimeWrap( void )
		{
			return Engine::instance().dtime();
		}

		Ref<ICamera> GetCameraWrap( void )
		{
			return Engine::instance().GetCamera();
		}

		// Engine functions.
		void ToggleFullScreenWrap( void )
		{
			Engine::instance().ToggleFullScreen();
		}
	}

	static void expose_core( void )
	{
		def( "resolution" , &ResolutionWrap );
		def( "time" , &GtimeWrap );
		def( "dtime" , &DtimeWrap );
		def( "callback" , &TimerSetWrap );
		def( "camera" , &GetCameraWrap );

		// engine functions.
		def( "ToggleFullScreen" , &ToggleFullScreenWrap );
	}

	namespace
	{
#define __declspec(X)

		BOOST_PYTHON_MODULE( xeres_engine )
		{
			expose_core();

			//expose_script();
		}

#undef __declspec
	}

	void expose_engine_to_python( void )
	{
		PythonPlug::instance().RegisterModule( "xeres" , init_module_xeres_engine );
	}
	
} // namespace xeres
